<template>     
    <avue-crud :option="option"
               v-model:search="search"
               v-model:page="page"
               v-model="form"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left> 
      </template>
      <template #index="{ row, index }">
                {{ page.pageSize*(page.currentPage -1) + index  + 1 }}  
      </template>
      <template #menu="scope">
        <el-button size="small"
                   plain 
                   @click="handleView(scope.row)">详情
        </el-button>
      </template>
      <template #lessonName="scope">
          <div>
            {{scope.row.lessonName}} = 
            <el-button  style="border:none;" size="small" icon="el-icon-edit" @click="showLessonEdit(item)"></el-button>   
          </div>
        
      </template>
    </avue-crud> 
    <transition name="drawer-transition">
        <div class="contain" v-show="display">
        <div class="drawerTage" @click="display = false">X</div>
        <div class="drawer">        
            <div> 教师:{{selRow.teacherNames}} </div>
            <div> 教室:{{selRow.roomName}} </div>
            <div> 扣课数:{{selRow.classHour}}课时 </div>
            <el-divider/>              
            <qyPreBook :lesson="selRow"></qyPreBook>          
        </div>
        </div>
    </transition>

</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/classes/qyClassesLessonsDetails";
  import option from "@/option/classes/qyClassesLessonsDetails";
  import {mapGetters} from "vuex";
  import {exportBlob} from "@/api/common";
  import {getToken} from '@/utils/auth';
  import {downloadXls} from "@/utils/util";
  import {dateNow} from "@/utils/date";
  import NProgress from 'nprogress';
  import 'nprogress/nprogress.css';
  import qyPreBook from '@/views/schedule/qyPreBook.vue'

  
  export default {
    components:{
        qyPreBook,
    },    
    props: {
        classes_id: String,       
    }, 
    data() {
      return {
        form: {},
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: option,
        display:false,
        selRow:{},
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.qyClassesLessonsDetails_add, false),
          viewBtn: this.validData(this.permission.qyClassesLessonsDetails_view, false),
          delBtn: this.validData(this.permission.qyClassesLessonsDetails_delete, false),
          editBtn: this.validData(this.permission.qyClassesLessonsDetails_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleExport() {
        let downloadUrl = `/xrsk-qyClasses/qyClassesLessonsDetails/export-qyClassesLessonsDetails?${this.website.tokenHeader}=${getToken()}`;
        const {
        } = this.query;
        let values = {
        };
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          NProgress.start();
          exportBlob(downloadUrl, values).then(res => {
            downloadXls(res.data, `课表详情${dateNow()}.xlsx`);
            NProgress.done();
          })
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      handleView(row){
        
        
        this.selRow = row;
        this.display = true;
        console.log("row==============",this.selRow );
      },
      onLoad(page, params = {}) {
        this.loading = true;

        const {
        } = this.query;

        let values = {
            classesId:this.classes_id
        };
        console.log(`output->ffffffffffffleesion_id`,values)

        getList(page.currentPage, page.pageSize, values).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>
 

<style lang="scss" scoped>
:deep(.avue-crud .el-table th.el-table__cell, .avue-crud th) {
    background-color: #F5F7FA;
}

:deep(.avue-crud__header) {
    display: none;
}
.title {
    font-weight: bold;
    font-size: 16px; 
    padding-top: 10px;
    align-items: center;
}
 
.drawer-transition-enter-active,
.drawer-transition-leave-active {
  transition: transform 0.3s ease;
}
.drawer-transition-enter, .drawer-transition-leave-to /* .drawer-transition-leave-active for <2.1.8 */ {
  transform: translateX(100%);
}
.contain {
  position: relative;
  display: fixed;
  position: fixed;
  top: 0;
  right: 0;
  width: 650px;
  height: 100vh;
  z-index: 1000;
}
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  padding: 10px 10px;
  width: 600px;
  height: 100vh;
  background-color: #fff;
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
  z-index: 1000;
}
.drawerTage {
  cursor: pointer;
  max-width: 100px;
  width: 100px;
  height: 50px;
  background-color: #fff;
  text-align: center;
  display: flex;
  align-items: center;
  border-radius: 10px 10px;
  padding-left: 10px;
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
}
</style>

